package com.media.selector.media.window

import android.content.Context
import android.content.ContextWrapper
import android.database.Cursor
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.PopupWindow
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.RecyclerView
import com.media.selector.R
import com.media.selector.media.adapter.MediaAlbumAdapter
import com.media.selector.media.model.entity.AlbumEntity
import com.media.selector.widget.recycler.MediaGridDecoration
import com.media.selector.widget.recycler.MediaLinearLayoutManager

/**
 * date        ：2020/4/27
 * author      ：蒙景博
 * description ：
 */
class AlbumSwitchWindow constructor(context: Context): ContextWrapper(context) {

    private var mRecyclerView: RecyclerView
    private val mAdapter by lazy { MediaAlbumAdapter() }
    private val mAlbumSelectWindow by lazy { PopupWindow(this) }

    private var mCursor: Cursor? = null
    private var mOnAlbumSwitchWindowStateListener: ((Boolean) -> Unit)? = null
    private var mOnAlbumItemClickListener: ((position: Int) -> Unit)? = null

    init {
        // 初始化PopupWindow
        mAlbumSelectWindow.setBackgroundDrawable(ColorDrawable(ContextCompat.getColor(this, R.color.media_color_primary)))
        mAlbumSelectWindow.height = ViewGroup.LayoutParams.WRAP_CONTENT
        mAlbumSelectWindow.width = getScreenWidth()
        mAlbumSelectWindow.isFocusable = true
        mAlbumSelectWindow.setOnDismissListener { mOnAlbumSwitchWindowStateListener?.invoke(false) }
        // 设置View
        val mContainerView = LayoutInflater.from(this).inflate(R.layout.window_album_select, null)
        mAlbumSelectWindow.contentView = mContainerView
        mRecyclerView = mContainerView.findViewById(R.id.window_album_recycler)
        mRecyclerView.layoutManager = MediaLinearLayoutManager(this)
        mRecyclerView.adapter = mAdapter
        mAdapter.setOnAlbumItemClickListener {
            mOnAlbumItemClickListener?.invoke(it)
            setSelection(it)
            mAlbumSelectWindow.dismiss()
        }
    }

    fun setOnAlbumSwitchWindowStateListener(listener: (Boolean) -> Unit) {
        this.mOnAlbumSwitchWindowStateListener = listener
    }

    fun setOnAlbumItemClickListener(listener: (position: Int)-> Unit) {
        this.mOnAlbumItemClickListener = listener
    }

    fun showAsDropDown(anchor: View) {
        mAlbumSelectWindow.showAsDropDown(anchor)
        mOnAlbumSwitchWindowStateListener?.invoke(true)
    }

    /**
     * 获取屏幕高度
     */
    private fun getScreenWidth(): Int = this.resources.displayMetrics.widthPixels

    fun setSelection(position: Int) {
        mAdapter.setSelection(position)
    }

    fun swapCursor(cursor: Cursor?) {
        this.mCursor = cursor
        val mAlbums = mutableListOf<AlbumEntity>()
        cursor?.use {
            while (it.moveToNext()) {
                mAlbums.add(AlbumEntity.valueOf(it))
            }
        }
        mAdapter.notifyAdapterChanged(mAlbums)
    }

    fun getCursor(): Cursor? = mCursor
}